<#import "/common.ftl" as common />
<@common.commomFrame >
	<div id="breadcrumb">
		<ul class="breadcrumb">
			 <li><i class="fa fa-home"></i><a href="index-2.html">首页</a></li>
			 <li class="active">黑板报</li>
		</ul>
	</div>
	<div class="padding-md " >
		<div class="panel panel-default ">
			<div class="panel-body">
				<div class="padding-md col-md-7 col-md-offset-2">
					<div class="row">
						<div class="panel panel-default">
							<div class="panel-heading">基本信息</div>
							<form  id="trainForm" class="form-horizontal form-border" method="post" action="/train/save">
								<input type="hidden" name="trainId" id="trainId" value="${(train.trainId)!}"/>
								<div class="form-group">
									<label class="col-md-2 control-label text-right"><span class="text-danger">*</span>车次名称</label>
									<div class="col-md-7 ">
										<input class="form-control input-sm" type="text" placeholder="输入车次名称" id="trainName" name="trainName" 
										   data-parsley-required="true" <#if (train.trainId)?? > readonly <#else> onblur="isExist(this)" </#if>
										   value="${(train.trainName)!}" />
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-2 control-label"><span style="color: red">*</span>始发站</label>
									<div class="col-md-3">
										<select id="startStationId" name="startStationId" class="form-control chzn-select">
											<option value="0" disabled selected >请选择始发站</option>
											<#if stationList?? && stationList?size gt 0>
												<#list stationList as station>
													<option value="${(station.stationId)!}" <#if (train.startStationId)?? && train.startStationId == station.stationId>selected</#if>>${(station.stationName)!}</option>
												</#list>
											</#if>
										</select>
									</div>
									<label class="col-md-2 control-label"><span style="color: red">*</span>终点站</label>
									<div class="col-md-3">
										<select id="endStationId" name="endStationId" class="form-control chzn-select" >
											<option value="0" disabled selected >请选择终点站</option>
											<#if stationList?? && stationList?size gt 0>
												<#list stationList as station>
													<option value="${(station.stationId)!}" <#if (train.endStationId)?? && train.endStationId == station.stationId>selected</#if>>${(station.stationName)!}</option>
												</#list>
											</#if>
										</select>
									</div>
								</div>	
								<div class="form-group">
									<label class="col-md-2 control-label"><span style="color: red">*</span>前方站</label>
									<div class="col-md-3">
										<select id="frontStationId" name="frontStationId" class="form-control chzn-select" >
											<option value="0" disabled selected >请选择前方站</option>
											<#if stationList?? && stationList?size gt 0>
												<#list stationList as station>
													<option value="${(station.stationId)!}" <#if (train.frontStationId)?? && train.frontStationId == station.stationId>selected</#if>>${(station.stationName)!}</option>
												</#list>
											</#if>
										</select>
									</div>
									<label class="col-md-2 control-label"><span style="color: red">*</span>后方站</label>
									<div class="col-md-3">
										<select id="afterStationId" name="afterStationId" class="form-control chzn-select" >
											<option value="0" disabled selected >请选择后方站</option>
											<#if stationList?? && stationList?size gt 0>
												<#list stationList as station>
													<option value="${(station.stationId)!}" <#if (train.afterStationId)?? && train.afterStationId == station.stationId>selected</#if>>${(station.stationName)!}</option>
												</#list>
											</#if>
										</select>
									</div>
								</div>
								<#-- 
								<div class="form-group">
									<label class="col-md-2 control-label"><span style="color: red">*</span>始发时间</label>
									<div class="col-md-4">
										<div class="input-group">
											<input id="startTime" name="startTimeStr" placeholder="请点击选择始发时间" class="form-control"
												 type="text" required  value="<#if (train.startTime)??>${train.startTime?string('HH:mm:ss')}</#if>"/>
											<span class="input-group-addon"><i class="fa fa-clock-o"></i></span>
										</div>
									</div>
									<label class="col-md-2 control-label"><span style="color: red">*</span>终点站时间</label>
									<div class="col-md-4">
										<div class="input-group">
											<input id="endTime" name="endTimeStr" placeholder="请点击选择终点站时间" class="form-control"
												   type="text" required  value="<#if (train.endTime)??>${train.endTime?string('HH:mm:ss')}</#if>"/>
											<span class="input-group-addon"><i class="fa fa-clock-o"></i></span>
										</div>
									</div>
								</div>
								 -->
								<div class="form-group">
									<label class="col-md-2 control-label"><span style="color: red">*</span>本站到达时间</label>
									<div class="col-md-4">
										<div class="input-group">
											<input id="reachTime" name="reachTimeStr" placeholder="请点击选择本站到达时间" class="form-control"
												   type="text" required  value="<#if (train.reachTime)??>${train.reachTime?string('HH:mm:ss')}</#if>"/>
											<span class="input-group-addon"><i class="fa fa-clock-o"></i></span>
										</div>
									</div>
									<label class="col-md-2 control-label"><span style="color: red">*</span>本站发车时间</label>
									<div class="col-md-4">
										<div class="input-group">
											<input id="leaveTime" name="leaveTimeStr" placeholder="请点击选择本站发车时间" class="form-control"
												   type="text" required value="<#if (train.leaveTime)??>${train.leaveTime?string('HH:mm:ss')}</#if>"/>
											<span class="input-group-addon"><i class="fa fa-clock-o"></i></span>
										</div>
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-2 control-label"><span class="text-danger">*</span>是否上水</label>
									<div class="col-md-4">
										<label class="label-radio inline">
											<input type="radio" name="isAddWater" value="1" required="required" <#if (train.isAddWater)?? && train.isAddWater == 1>checked</#if>>
											<span class="custom-radio" style="font-size:26px">是</span>
										</label>
										<label class="label-radio inline">
											<input type="radio" name="isAddWater" value="0" required="required" <#if (train.isAddWater)?? && train.isAddWater == 0>checked</#if> >
											<span class="custom-radio" style="font-size:26px;margin-left:6px">否</span>
										</label>
									</div>
									
									<label class="col-md-2 control-label"><span class="text-danger">*</span>是否去污</label>
									<div class="col-md-4">
										<label class="label-radio inline">
											<input type="radio" name="isDealRubbish" value="1" required="required" <#if (train.isDealRubbish)?? && train.isDealRubbish == 1>checked</#if> >
											<span class="custom-radio" style="font-size:26px">是</span>
										</label>
										<label class="label-radio inline">
											<input type="radio" name="isDealRubbish" value="0" required="required" <#if (train.isDealRubbish)?? && train.isDealRubbish == 0>checked</#if> >
											<span class="custom-radio" style="font-size:26px;margin-left:6px">否</span>
										</label>
									</div>
								</div>
								
								<div class="form-group">
									<label class="col-md-2 control-label"><span style="color: red">*</span>来车方向</label>
									<div class="col-md-3">
										<select name="comeDirectionId" id="comeDirectionId" class="form-control chzn-select" onchange="changeComeDirec()" >
											<option value="0" disabled selected >请选择来车方向</option>
											<#if comeDirectionList?? && comeDirectionList?size gt 0>
												<#list comeDirectionList as comeDirection>
													<option value="${(comeDirection.direcId)!}"  data-serial="${(comeDirection.serialNumber)!}"
													<#if (train.comeDirectionId)?? && train.comeDirectionId == comeDirection.direcId>selected</#if>>${(comeDirection.direcName)!}</option>
												</#list>
											</#if>
										</select>
									</div>
									<label class="col-md-2 control-label"><span style="color: red">*</span>发车方向</label>
									<div class="col-md-3">
										<select name="goDirectionId" id="goDirectionId" class="form-control chzn-select"  onchange="changeGoDirec()">
											<option value="0" disabled selected >请选择发车方向</option>
											<#if goDirectionList?? && goDirectionList?size gt 0>
												<#list goDirectionList as goDirection>
													<option value="${(goDirection.direcId)!}" data-serial="${(goDirection.serialNumber)!}"
													<#if (train.goDirectionId)?? && train.goDirectionId == goDirection.direcId>selected</#if>>${(goDirection.direcName)!}</option>
												</#list>
											</#if>
										</select>
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-4 control-label"><span style="color: red">*</span>接,发车股道</label>
									<div class="col-md-3">
										<select name="pickUpTrack" id="pickUpTrack" class="form-control chzn-select" onchange="changePickUpTrack()">
											<option value="0" disabled selected >请选择接车股道</option>
											<#if trackList?? && trackList?size gt 0>
												<#list trackList as track>
													<option value="${(track.trackId)!}" data-isAddWater="${(track.isAddWater)!}" data-isDealRubbish="${(track.isDealRubbish)!}"
														<#if (train.pickUpTrack)?? && train.pickUpTrack == track.trackId>selected</#if>>${(track.trackOrder)!}股道</option>
												</#list>
											</#if>
										</select>
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-2 control-label"><span style="color: red">*</span>接车路径序列</label>
									<div class="col-md-3">
										<input class="form-control input-sm" type="text" placeholder="请输入关键字,以逗号分隔" readonly
											 id="pickUpSequence"  name="pickUpSequence" value="${(train.pickUpSequence)!}"/>
									</div>
									<label class="col-md-2 control-label"><span style="color: red">*</span>发车路径序列</label>
									<div class="col-md-3">
										<input class="form-control input-sm" type="text" readonly id="departureSequence" name="departureSequence" value="${(train.departureSequence)!}"/>
									</div>
								</div>
								<div class="panel-footer text-center">
									<button type="button" class="btn btn-success" id="submitButtonSave" onclick="saveTrain()" >保存</button>
									<button type="button" class="btn btn-default" onclick="history.go(-1);">返回</button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<#-- 
	<link href="${staticPath}/js/timepicker/jquery-ui-timepicker-addon.min.css" rel="stylesheet" type="text/css"/>
	<script src="${staticPath}/js/timepicker/jquery-ui-timepicker-addon.min.js" type="text/javascript"></script>
	<script src="${staticPath}/js/timepicker/jquery-ui-timepicker-zh-CN.js" type="text/javascript"></script>
	 
	<script>
		var calendartime = new lCalendar();
		calendartime.init({
			'trigger': '#demo3',
			'type': 'time'
		});
		var calendartime = new lCalendar();
		calendartime.init({
			'trigger': '#endTime',
			'type': 'time'
		});
		var calendartime = new lCalendar();
		calendartime.init({
			'trigger': '#reachTime',
			'type': 'time'
		});
		var calendartime = new lCalendar();
		calendartime.init({
			'trigger': '#leaveTime',
			'type': 'time'
		});
	</script>
	-->
	<script language="javascript">
		//判断车次名称是否已经存在
		function isExist(obj){
			var trainName = obj.value;
			if(trainName != ""){
				$.ajax({
					cache: true,
					type: "POST",
					url:"/train/isExist",
					data:"trainName=" + trainName,
					async: false,
					error: function(request) {
						alertify.alert("错误：服务器异常！");
					},
					success: function(data) {
						if(data.success){
							return;
						}else{
							alertify.alert("此车次名称已经存在,请重新确认！！！");
						}
					}
				});
			}
		}
	
		//修改接车路径序列
		function changeComeDirec(){
			//接车股道
			var trackId = $('#pickUpTrack option:selected') .val();
			//所选接车方向的序列编号
			var comeDireSerial = $('#comeDirectionId option:selected').attr('data-serial');
			var newSerial = comeDireSerial.replace('?',trackId);
			$("#pickUpSequence").val(newSerial);
		}
	
		//修改发车路径序列
		function changeGoDirec(){
			//发车股道
			var trackId = $('#pickUpTrack option:selected') .val();
			//所选发车方向的序列编号
			var goDireSerial = $('#goDirectionId option:selected').attr('data-serial');
			var newSerial = goDireSerial.replace('?',trackId);
			$("#departureSequence").val(newSerial);
		}
	
		//修改接发车股道
		function changePickUpTrack(){
			var trackId = $('#pickUpTrack option:selected') .val();
			var track_isAddWater = $('#pickUpTrack option:selected') .attr('data-isAddWater');
			var track_isDealRubbish = $('#pickUpTrack option:selected') .attr('data-isDealRubbish');
			
			var isAddWater = $('input[name="isAddWater"]:checked').val();
			var isDealRubbish = $('input[name="isDealRubbish"]:checked').val();
			
			var comeDireSerial = $('#comeDirectionId option:selected').attr('data-serial');
			var newSerial = comeDireSerial.replace('?',trackId);
			$("#pickUpSequence").val(newSerial);
			
			var goDireSerial = $('#goDirectionId option:selected').attr('data-serial');
			var gonewSerial = goDireSerial.replace('?',trackId);
			$("#departureSequence").val(gonewSerial);
			
			var tip = "";
			if(track_isAddWater != "" && isAddWater != undefined && isAddWater == "1" && track_isAddWater != isAddWater){
				tip += "所选股道不支持上水！！！"
			}
			if(track_isDealRubbish != "" && isDealRubbish != undefined && isDealRubbish == "1" && track_isDealRubbish != isDealRubbish){
				tip += "所选股道不支持去污！！！";
			}
			if(tip != ""){
				alertify.alert("提示：" + tip);
			}
		
		}
	
		//保存修改后的信息
		function saveTrain(){
			var trainName = $("#trainName").val();
			if($.trim(trainName) == "" ){
				alertify.alert("请输入车次名称。");
				return false;
			}
			var startStationId = $('#startStationId option:selected') .val();
			if($.trim(startStationId) == "" || startStationId == "0" ){
				alertify.alert("请选择有效的始发站。");
				return false;
			}
			var endStationId = $('#endStationId option:selected') .val();
			if($.trim(endStationId) == "" || endStationId == "0" ){
				alertify.alert("请选择有效的终点站。");
				return false;
			}
			var frontStationId = $('#frontStationId option:selected') .val();
			if($.trim(frontStationId) == "" || frontStationId == "0" ){
				alertify.alert("请选择有效的前方站。");
				return false;
			}
			var afterStationId = $('#afterStationId option:selected') .val();
			if($.trim(afterStationId) == "" || afterStationId == "0" ){
				alertify.alert("请选择有效的后方站。");
				return false;
			}	
			var reachTime = $("#reachTime").val();
			if($.trim(reachTime) == "" ){
				alertify.alert("请输入本站到达时间。");
				return false;
			}			
			var leaveTime = $("#leaveTime").val();
			if($.trim(leaveTime) == "" ){
				alertify.alert("请输入本站离站时间。");
				return false;
			}					
			var isAddWater = $('input[name="isAddWater"]:checked').val();
			if($.trim(isAddWater) == "" ){
				alertify.alert("请选择是否上水。");
				return false;
			}
			var isDealRubbish = $('input[name="isDealRubbish"]:checked').val();							
			if($.trim(isDealRubbish) == "" ){
				alertify.alert("请选择是否去污。");
				return false;
			}									
			var comeDirectionId = $('#comeDirectionId option:selected') .val();											
			if($.trim(comeDirectionId) == "" || comeDirectionId == "0" ){
				alertify.alert("请选择有效的来车方向。");
				return false;
			}													
			var goDirectionId = $('#goDirectionId option:selected') .val();											
			if($.trim(goDirectionId) == "" || goDirectionId == "0" ){
				alertify.alert("请选择有效的发车方向。");
				return false;
			}
			var pickUpTrack = $('#pickUpTrack option:selected') .val();											
			if($.trim(pickUpTrack) == "" || pickUpTrack == "0" ){
				alertify.alert("请选择有效的股道。");
				return false;
			}
																						
			$.ajax({
				cache: true,
				type: "POST",
				url:"/train/save",
				data:$('#trainForm').serialize(),
				async: false,
				error: function(request) {
					alertify.alert("错误：服务器异常1234！");
				},
				success: function(data) {
					if(data.success){
						location.href="/train/list";
					}else{
						alertify.alert("错误:" + data.message);
					}
				}
			});
		}
	</script>
</@common.commomFrame>